% reversal of the rank 
% % Focus on the rank of schools 11 (small) and 10 (big) 
% % for a non-disadvanaged student with equal distance to both schools

tic 
cd(folder_data)
load est_Mis.mat LL_ST_est LL_TT_est theta_true M P


% expected quaility of schools 11 and 10
V = NaN(size(LL_ST_est,2),2,2);
V(:,1,1) = LL_ST_est(1,:)*11 + LL_ST_est(2,:)*1 + LL_ST_est(4,:)*1; % school 11, stability
V(:,1,2) = LL_TT_est(1,:)*11 + LL_TT_est(2,:)*1 + LL_TT_est(4,:)*1; % school 11, TT

V(:,2,1) = LL_ST_est(1,:)*10 + LL_ST_est(2,:)*0 + LL_ST_est(4,:)*0; % school 10, stability
V(:,2,2) = LL_TT_est(1,:)*10 + LL_TT_est(2,:)*0 + LL_TT_est(4,:)*0; % school 10, stability

rank = NaN(4,3);
true_rank = exp(theta_true(1)*11+theta_true(2)*1)/(exp(theta_true(1)*11+theta_true(2)*1)+exp(theta_true(1)*10+theta_true(2)*0)); 
for pp = 1:P
    for jj = 1:2
        rank(jj,pp) = (mean( exp(V((pp-1)*M+1:pp*M,1,jj)) ...
            ./ (exp(V((pp-1)*M+1:pp*M,2,jj)) + exp(V((pp-1)*M+1:pp*M,1,jj))))...
            ); 
    end
end
rank(3,:) = true_rank;

% figure 
figure()
% area([2;3;4],[1*ones(3,1)],'FaceColor',[0.9  0.9  0.9],'LineStyle','none');
% hold on
x = 1:P;
p1 = plot(x,rank(2,:),'-r','LineWidth',2);
hold on
p2 = plot(x,rank(1,:),':b','LineWidth',2);
hold on
p3 = plot(x,rank(3,:),'--m','LineWidth',2);
hold on
legend([p1,p2,p3],{'WTT Estimates', 'Stability Estimates','True value'},'Box','off','Location','southwest')

%title(figname)
ax = gca; % current axes
ax.FontSize = 14;
ax.TickDir = 'out';
ax.TickLength = [0.01 0.01];
ax.YLim = [0.77 0.92];
% ax.XLim = [1 4];
% ax.XTick = ([1 2 3 4]);
% ax.XTickLabel = ({'TRS','IRR1','IRR2','REL'});
ax.XLim = [1 3];
ax.XTick = ([1 2 3]);
ax.XTickLabel = ({'TT','PIM','PRM'});
xlabel('Data Generating Processes');
ylabel('Fraction of Applicants Preferring 11 to 10');
cd(folder_figures)
print('fig_2_rank_reversal','-djpeg')
print('fig_2_rank_reversal','-depsc2')

toc